#ifndef __KV_CONFIG_DATA_H__
#define __KV_CONFIG_DATA_H__

#include <string>
#include <map>
#include <sstream>

namespace KVConfig
{
    typedef std::map<std::string, std::string> KVMap;

    class Data
    {
    public:
        bool has(const std::string &key) const;
        std::string get(const std::string &key) const;

        void set(const std::string &key, const std::string &value);
        void clear();

        template <typename T>
        bool get(const std::string &key, T &value) const;
        bool get(const std::string &key, std::string &value) const;
        bool get(const std::string &key, bool &value) const;

    public:
        std::string operator[](const std::string &key) const;

    private:
        KVMap m_data;
    };

    template <typename T>
    bool Data::get(const std::string &key, T &value) const
    {
        KVMap::const_iterator iter = m_data.find(key);
        if (iter != m_data.end()) {
            std::stringstream ss(iter->second);
            try {
                ss >> value;
                return true;
            } catch (...) {
                return false;
            }
        } else {
            return false;
        }
    }
}
#endif  //__KV_CONFIG_DATA_H__
